﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using WFExtended.Activities.Designers;
using WFExtended.Framework.ServiceModel;

namespace WFExtended.Activities
{
    /// <summary>
    /// Build a WCF Proxy with a Service Model Client Endpoint Configuration Name
    /// <code>
    /// DelegateInArgument<WcfProxy<IMockService>> proxy = new DelegateInArgument<WcfProxy<IMockService>>(); <br />
    /// Activity workflow = new WcfConfigurationProxy<IMockService>() <br />
    /// &nbsp; { <br />
    /// &nbsp; &nbsp; ConfigurationName = "myEndpoint" <br />
    /// &nbsp; &nbsp; Body = new System.Activities.ActivityAction<WcfProxy<IMockService>>() <br />
    /// &nbsp; &nbsp; { <br />
    /// &nbsp; &nbsp; &nbsp; Argument = proxy, <br />
    /// &nbsp; &nbsp; &nbsp; Handler = new Sequence() <br />
    /// &nbsp; &nbsp; &nbsp; { <br />
    /// &nbsp; &nbsp; &nbsp; } <br />
    /// &nbsp; &nbsp; } <br />
    ///&nbsp;  } <br />
    /// </code>
    /// </summary>
    /// <typeparam name="T">Type of the Service Contract</typeparam>
    [ToolboxBitmapAttribute(typeof(EntryPoint), "Resources.WcfContractProxyToolbox.bmp")]
    [Designer(typeof(WcfContractProxyDesigner))]
    [ContentProperty("Body")]
    [Description("Build a WCF Proxy with a Service Model Client Endpoint Configuration Name for the service contract T")]
    public class WcfConfigurationProxy<T> :  AWcfProxyActivity<T>
        where T : class
    {
        /// <summary>
        /// Get or Set the Endpoint Configuration Name
        /// <remarks>
        /// Required Argument
        /// </remarks>
        /// </summary>
        [Browsable(true)]
        [RequiredArgument]
        [Category("Endpoint")]
        [Description(" Get or Set the Endpoint Configuration Name")]
        public InArgument<string> ConfigurationName { get; set; }

        /// <summary>
        /// Get or Set the list of Headers thant will be send for each request
        /// </summary>
        [Browsable(false)]
        public new InArgument<List<AddressHeader>> Headers { get; set; }

        /// <summary>
        /// Get the Service Endpoint
        /// </summary>
        /// <param name="context">The execution context in which the activity executes</param>
        /// <returns>>Return the Service Endpoint</returns>
        protected override System.ServiceModel.Description.ServiceEndpoint GetServiceEndpoint(System.Activities.NativeActivityContext context)
        {
            return new ServiceEndpointConfiguration(ContractDescription.GetContract(typeof(T)), ConfigurationName.Get(context));
        }
    }
}
